
Page({
  data: {
    oldaddress:"",  // 身份证号
    oldphone:"",    // 设备码
    region:[],
    navtop:{},
    openid:"",
    lrsfz:"",
    arr:[],
    id:"",
    sblrsfz:"",
  },
  onLoad(){
    this.setData({
      openid:getApp().globalData.openid
    })
    // 导航栏
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    //3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navtop:menuButton.top
    })
  },
  onShow(){
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/getProfile',
      method:"POST",
      data:{
        openid:this.data.openid
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        console.log(res);
        this.setData({
          oldaddress:res.data.data.laoren[0].sfz,
        })
      }
    })
    // 获取设备列表
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/deviceList',
      method:"POST",
      data:{
        openid:this.data.openid
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        console.log(res);
        const app = getApp()
        console.log(app.globalData);
        this.setData({
            arr:res.data.data,
            sblrsfz:app.globalData.sblrsfz,
            },()=>{
                // this.data.arr.forEach(element => {
                //     console.log(element.xm);
                //     if(element.xm.length <= 1){
                //         wx.showModal({
                //             title: '请前往老人圈完善老人信息',
                //             showCancel: false, // 不显示取消按钮
                //         });
                //         return;
                //     }
                // });
                
        
        })
      }
    
    })
  },
   //返回上一个页面
   gethui() {
    wx.navigateBack({
        delta: 1
    })
},
  bindRegionChange: function (e) {
    console.log(e.detail.value)
    console.log(e.detail.code)
    this.setData({
      region: e.detail.value
    })
  },
  gitfor(){
    wx.navigateBack({
      delta: -1
    })
  },
  oldaddress(e){
    this.setData({
      oldaddress:e.detail.value
    })
    const app = getApp()
    app.globalData.sblrsfz = e.detail.value
    
  },
  oldphone(e){
    this.setData({
      oldphone:e.detail.value
    })
  },
  shanchu(e){
    this.data.arr.splice(e.currentTarget.dataset.index,1)
    this.setData({
      arr:this.data.arr
    })
    this.setData({
      id:e.currentTarget.dataset.id
    })
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/deviceUnbind',
      method:"POST",
      data:{
        openid:this.data.openid,
        device_id:this.data.id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        console.log(res);
      }
    })
    wx.showToast({
      title: '删除成功',
      icon: 'success'
    });
  },
  formSubmit: function(e) {
    console.log(e);
    const { sbm, sblrsfz } = e.detail.value;
    if (!sbm.trim() ) {
      wx.showToast({
        title: '设备码不能为空',
        icon: 'none'
      });
      return false; 
    }
    function isValidString(str) {
      const regex = /^[a-zA-Z0-9]+$/;
      return regex.test(str);
    }
    if(!isValidString(this.data.oldphone)){
      console.log(isValidString(!this.data.oldphone));
      wx.showToast({
        title: '请输入正确的设备码',
        icon: 'none'
      });
      return false; 
    }
    if ( !sblrsfz.trim()) {
      wx.showToast({
        title: '身份证号不能为空',
        icon: 'none'
      });
      return false; 
    }
  
    // 校验身份证号
    if (!/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(sblrsfz)) {
      wx.showToast({
        title: '身份证号格式不正确',
        icon: 'none'
      });
      return false; 
    }
    wx.request({
      url: 'https://dj.ifankui.com/api/usrEn/deviceBind',
      method:'POST',
      data:{
        openid:this.data.openid,
        sfz:sblrsfz,
        device_code:this.data.oldphone
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success:(res)=>{
        console.log(res);
        wx.showToast({
            title: res.data.msg,
            icon: 'success'
        });
            this.onShow()
            wx.showModal({
                title: '请前往信息绑定页面完善信息',
                showCancel: false, // 不显示取消按钮
            });
      }
    })
   
    
  },
  onScanCode: function() {
    wx.scanCode({
      success: (res) => {
        // 扫码成功后，res.result 是扫码结果
        console.log(res.result);
        let newdevice = res.result
        let result = newdevice.split(",");
        console.log(result);
        wx.showToast({
              title: '扫描成功',
              icon: 'success',
              duration: 2000
            });
            this.setData({
            oldphone:result[0],
            sblrsfz:result[1]
        })
      },
      fail: (err) => {
        // 扫码失败
        console.error('扫描失败:', err);
        wx.showToast({
          title: '扫描失败',
          icon: 'none',
          duration: 2000
        });
      }
    });
  }
});